<?php
/**
 * Created by PhpStorm.
 * User: hantian
 * Date: 2017/6/18
 * Time: 下午12:22
 */

namespace App\Service;

use App\Models\Article;
use App\Models\ArticleClassify;

final class ArticleService
{
    public static function createArticle($info)
    {
        $info['status'] = Article::STATUS_OFF;
        return Article::create($info);
    }

    public static function deleteArticle($id)
    {
        return Article::where('id', $id)->delete();
    }

    public static function updateArticle($id, $info)
    {
        return Article::where('id', $id)->update($info);
    }

    public static function getArticleList($condition, $page_limit)
    {
        return Article::where($condition)
            ->orderby('created_at', 'desc')
            ->paginate($page_limit)->toarray();
    }


    public static function createArticleClass($user_id, $name)
    {
        $info = array();
        $info['user_id'] = $user_id;
        $info['name'] = $name;
        $count = ArticleClassify::where($info)->count();
        if ($count > 0) {
            return false;
        } else {
            return ArticleClassify::create($info);
        }
    }

    public static function getArticleClassIdByClassName($class_name, $user_id)
    {
        $condition = array();
        $condition['user_id'] = $user_id;
        $condition['name'] = $class_name;
        return ArticleClassify::where($condition)->value('id');

    }

    public static function deleteArticleClass($id)
    {
        return ArticleClassify::where('id', $id)->delete();
    }

    public static function updateArticleClass($id, $info)
    {
        return ArticleClassify::where('id', $id)->update($info);
    }

    public static function getArticleClassListByUser($user_id)
    {
        return ArticleClassify::where('user_id', $user_id)
            ->orderby('created_at', 'desc')
            ->get();
    }

}
